package feeinterface.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import com.ulwx.database.DataBaseSet;
import com.ulwx.database.RowMapper;
import com.ulwx.tool.DbUtils;
import com.ulwx.type.TInteger;

import feeinterface.modeldb.ServiceCoUserDoublePrecision;

/**
 * 业务合作用户双精使用金额（月清）
 * 
 * @author 黄杰
 * 
 */
public class ServiceCoUserDoublePrecisionDao {
	/**
	 * 
	 */
	private static final Logger log = Logger
			.getLogger(ServiceCoUserDoublePrecisionDao.class);

	/**
	 * 对双精使用金额的累加
	 * 
	 * @param scd
	 */
	public static void Cumsum(ServiceCoUserDoublePrecision scd) {
		try {
			String sql = "INSERT INTO ServiceCoUserDoublePrecision(IMSI,ServiceCoID,DoubleFee,UpdateTime)"
					+ " VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE DoubleFee=DoubleFee+?,UpdateTime=?";
			Map<Integer, Object> map = new HashMap<Integer, Object>();
			map.put(1, scd.getIMSI());
			map.put(2, scd.getServiceCoID());
			map.put(3, scd.getDoubleFee());
			map.put(4, scd.getUpdateTime());
			map.put(5, scd.getDoubleFee());
			map.put(6, scd.getUpdateTime());
			DbUtils.executeBindUpdate(null, sql, map);
		} catch (Exception e) {
			log.error("", e);

		}
	}
	
	/**
	 * 对无上行通道的使用金额的累加
	 * 
	 * @param scd
	 */
	public static void sumWithNoMo(ServiceCoUserDoublePrecision scd) {
		try {
			String sql = "INSERT INTO ServiceCoUserDoublePrecision" +
					"(IMSI,ServiceCoID,ChannelNoMoFee,UpdateTime)"
					+ " VALUES (?,?,?,?) ON DUPLICATE KEY" +
					" UPDATE ChannelNoMoFee=ChannelNoMoFee+?,UpdateTime=?";
			Map<Integer, Object> map = new HashMap<Integer, Object>();
			map.put(1, scd.getIMSI());
			map.put(2, scd.getServiceCoID());
			map.put(3, scd.getChannelNoMoFee());
			map.put(4, scd.getUpdateTime());
			map.put(5, scd.getChannelNoMoFee());
			map.put(6, scd.getUpdateTime());
			DbUtils.executeBindUpdate(null, sql, map);
		} catch (Exception e) {
			log.error("", e);

		}
	}

	/**
	 * 根据IMSI和合作ID获取双精金额（单位分），
	 * 
	 * 双精金额:计费接口选通道累加，通道接口减去
	 * 
	 * @param imsi
	 * @param serviceCoID
	 * @return
	 */
	public static int getDoubleFee(String imsi, Integer serviceCoID, TInteger channelNoMoFee) {
		String sql = "SELECT * FROM ServiceCoUserDoublePrecision WHERE IMSI=? AND ServiceCoID=?";
		Map<Integer, Object> args = new HashMap<Integer, Object>();
		try {
			args.put(1, imsi);
			args.put(2, serviceCoID);
//			RowMapper<Tw> rowMapper = new RowMapper<Integer>() {
//				public Integer mapRow(DataBaseSet rs) throws Exception {
//					
//					return rs.getInt("DoubleFee");
//				}
//			};
			DataBaseSet rs = DbUtils.doCachedQuery(null, sql, args);
			if(rs.next()){
				channelNoMoFee.setValue(rs.getInt("ChannelNoMoFee"));
				return rs.getInt("DoubleFee");
			}else{
				channelNoMoFee.setValue(0);
			}
		} catch (Exception e) {
			channelNoMoFee.setValue(0);
			log.error("", e);
		}
		return 0;
	}
	
	public static void main(String[] args) {
		//int ii=(int)15.9999999999999456;
		//System.out.println(ii);
	}
}
